package top.kaoshanji.p1ch03b;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.StandardCharsets;

/**
 * 代码清单 p1ch03b-02：阻塞式服务器端套接字的使用示例
 * author kaoshanji
 * ^_^
 * create time 2025/1/8 19:20
 */
public class ServerSocketChannelExample01 {

    private static final Logger logger = LogManager.getLogger(ServerSocketChannelExample01.class);

    public static void main(String[] args) throws IOException {
        logger.info("...代码清单 p1ch03b-02：阻塞式服务器端套接字的使用示例...");

        ServerSocketChannel channel = ServerSocketChannel.open();
        channel.bind(new InetSocketAddress("localhost", 10800));
        while (true) {
            try (SocketChannel sc = channel.accept()){
                sc.write(ByteBuffer.wrap("Hello".getBytes(StandardCharsets.UTF_8)));
            }
        }


    }

}
